package com.lottery.service;

import com.lottery.dao.OrderDao;
import com.lottery.dto.AppOrdersOutputDTO;
import com.lottery.dto.OrdersAndBetNumbersDTO;
import com.lottery.dto.TotalCheckOutputDTO;
import com.lottery.pojo.TableBetNumbers;
import com.lottery.pojo.TableOrders;
import com.lottery.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;

    public List<TableOrders> getAllOrders() {
        return orderDao.getAllOrders();
    }

    public List<OrdersAndBetNumbersDTO> getPureOrders(String machine_number, String serial_number, String startTime, String endTime) {
        return orderDao.getPureOrders(machine_number, serial_number, startTime, endTime);
    }

    public List<TableBetNumbers> getBetsByDate(String startTime, String endTime, String bet_number) {
        return orderDao.getBetsByDate(startTime, endTime, bet_number);
    }

    public List<TableOrders> getPureOrdersByDate(String date) {
        return orderDao.getPureOrdersByDate(date);
    }

    public List<OrdersAndBetNumbersDTO> getPureOrdersBySerialNumber(String serial_number) {
        return orderDao.getPureOrdersBySerialNumber(serial_number);
    }

    public List<OrdersAndBetNumbersDTO> getPureBetNumbers(@Param("numbers") List<OrdersAndBetNumbersDTO> numbers) {
        return orderDao.getPureBetNumbers(numbers);
    }

    public List<OrdersAndBetNumbersDTO> getOrders(String bet_number, String machine_number, String serial_number, Integer is_winner, String startTime, String endTime, String belong_to, String b_is_winner, String checked_winning) {
        if (belong_to != null && belong_to.contains(",")) {
            String[] belongs = belong_to.split(",");
            String b1 = "", b2 = "", b3 = "", b4 = "";
            if (belongs.length == 1) {
                b1 = belongs[0];
            } else if (belongs.length == 2) {
                b1 = belongs[0];
                b2 = belongs[1];
            } else if (belongs.length == 3) {
                b1 = belongs[0];
                b2 = belongs[1];
                b3 = belongs[2];
            } else if (belongs.length == 4) {
                b1 = belongs[0];
                b2 = belongs[1];
                b3 = belongs[2];
                b4 = belongs[3];
            }
            long l1 = System.currentTimeMillis();
            log.info("查询订单getOrdersW()开始:{},{},{},{}", b1, b2, b3, b4);
            List<OrdersAndBetNumbersDTO> ordersW = orderDao.getOrdersW(bet_number, machine_number, serial_number, is_winner, startTime, endTime, b1, b2, b3, b4, b_is_winner, checked_winning);
            log.info("查询订单getOrdersW()结束:{}", System.currentTimeMillis() - l1);
            return ordersW;
        }
        return orderDao.getOrders(bet_number, machine_number, serial_number, is_winner, startTime, endTime, belong_to, b_is_winner, checked_winning);
    }

    public List<OrdersAndBetNumbersDTO> getOrdersByMachineNumber(String machine_number, String startTime, String endTime) {
        return orderDao.getOrdersByMachineNumber(machine_number, startTime, endTime);
    }

    public int deleteOrderById(int id) {
        return orderDao.deleteOrderById(id);
    }

    public int deleteOrderBySerialNumber(String serial_number) {
        return orderDao.deleteOrderBySerialNumber(serial_number);
    }

    public void deleteOrderByDateRange(String startTime, String endTime) {
        orderDao.deleteBetByDateRange(startTime, endTime);
        orderDao.deleteOrderByDateRange(startTime, endTime);
    }

    public List<TotalCheckOutputDTO> getTotalCheck() {
        return orderDao.getTotalCheck();
    }

    public List<AppOrdersOutputDTO> appGetOrders(String machine_number, String serial_number, Integer is_winner, String startTime, String endTime) {
        return orderDao.appGetOrders(machine_number, serial_number, is_winner, startTime, endTime);
    }

    public void updateOrderPaid(String id) {
        orderDao.updateOrderPaid(id, DateUtils.getSub4DT());
    }

    public void insertOrder(TableOrders order, String dt) {
        order.setCreated_at(dt);
        order.setUpdated_at(dt);
        orderDao.insertOrder(order);
    }

    public void insertBetNumber(TableBetNumbers number, String dt) {
        number.setCreated_at(dt);
        number.setUpdated_at(dt);
        orderDao.insertBetNumber(number);
    }

    ///
    public List<TableOrders> getOrdersByDate(int is_deleted, String startTime, String EndTime) {
        return orderDao.getOrdersByDate(is_deleted, startTime, EndTime);
    }

    public List<TableBetNumbers> getBetsByOrderId(int order_id) {
        return orderDao.getBetsByOrderId(order_id);
    }

    public void updateBet(int order_id) {
        orderDao.updateBet(order_id);
    }

    public void updateOrder(int id) {
        orderDao.updateOrder(id);
    }

    public List<TableOrders> getOrderByDeletedAndBelong(String region_key) {
        return orderDao.getOrderByDeletedAndBelong(region_key);
    }

    public List<TableBetNumbers> getBets432(int order_id, int is_pool, String bet_number, String startTime, String endTime) {
        return orderDao.getBets432(order_id, is_pool, bet_number, startTime, endTime);
    }

    public void updateBet432(int order_id, int rank, double prizes, String winning_payment_time) {
        orderDao.updateBet432(order_id, rank, prizes, winning_payment_time);
    }

    public TableOrders getOrderById(int id) {
        return orderDao.getOrderById(id);
    }

    public void updateOrder432(int id, double total_winning_amount) {
        orderDao.updateOrder432(id, total_winning_amount);
    }
}
